package com.xlf.service.judge.strategy.judgeProblemMode.impl;

import com.xlf.pojo.constant.Constants;
import com.xlf.service.judge.strategy.judgeProblemMode.AbstractJudgeProblemModeStrategy;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

/**
 * @Author xlf 2734029211@qq.com
 * @Date 2023/9/21 10:56
 * @Description: TODO 默认采用文本md5对比的方式进行判断是否通过案例。
 * @Version 1.0
 */
@Service
public class DefaultJudgeProblemMode extends AbstractJudgeProblemModeStrategy {


    /**
     * 默认采用对比md5来判断是否通过案例。
     *
     * @param userOutput       用户输出
     * @param isRemoveEOLBlank 是否除去尾部空格
     * @param testcase         测试用例
     * @return 题目状态
     */
    @Override
    protected Integer compareOutput(String userOutput, Integer isRemoveEOLBlank, String testcase) {

        if (isRemoveEOLBlank.equals(1)) {
            userOutput = rtrim(userOutput);
            testcase = rtrim(testcase);
        }

        String userOutputMd5 = DigestUtils.md5DigestAsHex(userOutput.getBytes(StandardCharsets.UTF_8));
        String testcaseMd5 = DigestUtils.md5DigestAsHex(testcase.getBytes(StandardCharsets.UTF_8));

        if (userOutputMd5.equals(testcaseMd5))
            return Constants.Judge.STATUS_ACCEPTED.getStatus();

        // 如果不AC,进行PE判断，否则为WA
        userOutputMd5 = DigestUtils.md5DigestAsHex(userOutput.replaceAll("\\s+", "").getBytes(StandardCharsets.UTF_8));

        if (userOutputMd5.equals(testcaseMd5)) {
            return Constants.Judge.STATUS_PRESENTATION_ERROR.getStatus();
        } else {
            return Constants.Judge.STATUS_WRONG_ANSWER.getStatus();
        }

    }
}
